﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Intex.Db.Models;
using PrintAgentService;

namespace PrintingManagerSystem
{
    public partial class ItemTypeMaintenance : Form
    {
        public ItemTypeMaintenance()
        {
            InitializeComponent();
        }

        Dictionary<string, ItemType> fundamentalDataDic = new Dictionary<string, ItemType>();
        private void ItemTypeMaintenance_Load(object sender, EventArgs e)
        {
            Init();

            GetData();

            BindingData();
        }

        private void BindingData()
        {
            this.dgvFundamentalData.Rows.Clear();
            if (fundamentalDataDic.Count > 0)
            {
                foreach (var data in fundamentalDataDic)
                {
                    int index = this.dgvFundamentalData.Rows.Add();
                    this.dgvFundamentalData.Rows[index].Cells["ItemTypeId"].Value = data.Value.ItemTypeId;
                    this.dgvFundamentalData.Rows[index].Cells["Alias"].Value = data.Value.Alias;
                    this.dgvFundamentalData.Rows[index].Cells["ItemName"].Value = data.Value.ItemName;
                    this.dgvFundamentalData.Rows[index].Cells["ItemDataType"].Value = data.Value.ItemDataType;
                    this.dgvFundamentalData.Rows[index].Tag = data.Value;
                }
                this.dgvFundamentalData.Rows[0].Selected = true;
            }
        }

        private void Init()
        {
            DataTable dtItemDataType = new DataTable();
            dtItemDataType.Columns.Add("colText");
            dtItemDataType.Columns.Add("colValue");
            dtItemDataType.Rows.Add(new object[] { "ImportDataField", "ImportDataField" });
            dtItemDataType.Rows.Add(new object[] { "DataField", "DataField" });
            dtItemDataType.Rows.Add(new object[] { "joinDataFieldImage", "joinDataFieldImage" });
            dtItemDataType.Rows.Add(new object[] { "joinDataFieldString", "joinDataFieldString" });
            this.cmbItemDataType.ValueMember = "colValue";
            this.cmbItemDataType.DisplayMember = "colText";
            this.cmbItemDataType.DataSource = dtItemDataType;
        }

        private void GetData()
        {
            fundamentalDataDic.Clear();
            string strPrefix = System.Configuration.ConfigurationManager.AppSettings["FundamentalDataColumnPrefix"]; //Properties.Settings.Default.FundamentalDataColumnPrefix;
            PrintAgent pa = new PrintAgent();
            int colNum = 0;
            if (int.TryParse(System.Configuration.ConfigurationManager.AppSettings["FundamentalDataColumnNumber"], out colNum))
            {
                for (int i = 0; i < colNum; i++)
                {
                    string alias = strPrefix + i.ToString();
                    var itemtypeObj = pa.GetItemTypeByAlias(alias);
                    if (itemtypeObj != null)
                    {
                        fundamentalDataDic.Add(alias, itemtypeObj);
                    }
                }
            }
            
        }

        private ItemType _selectedItem;

        public ItemType selectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                if (value == null)
                {
                    this.groupBox1.Enabled = false;
                    this.lbAlias.Text = "";
                    this.txtItemName.Text = "";
                }
                else
                {
                    this.groupBox1.Enabled = true;
                    this.lbAlias.Text = selectedItem.Alias;
                    this.txtItemName.Text = selectedItem.ItemName;
                    this.cmbItemDataType.SelectedValue = selectedItem.ItemDataType;
                }
            }
        }

        int nowIndex = -1;

        private void dgvFundamentalData_SelectionChanged(object sender, EventArgs e)
        {
            nowIndex = -1;
            selectedItem = null;
            if (this.dgvFundamentalData.SelectedRows.Count > 0)
            {
                selectedItem = this.dgvFundamentalData.SelectedRows[0].Tag as ItemType;
                nowIndex = this.dgvFundamentalData.SelectedRows[0].Index;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (this.selectedItem != null)
            {
                if (string.IsNullOrWhiteSpace(this.txtItemName.Text))
                {
                    MessageBox.Show("请填写有效的item name");
                    return;
                }
                PrintAgent pa = new PrintAgent();
                this.selectedItem.ItemName = this.txtItemName.Text;
                this.selectedItem.ItemDataType = this.cmbItemDataType.SelectedValue.ToString();
                if (pa.SaveItemType(this.selectedItem))
                {
                    GetData();
                    BindingData();
                    if (nowIndex > 0 && this.dgvFundamentalData.Rows.Count > nowIndex)
                    {
                        this.dgvFundamentalData.Rows[nowIndex].Selected = true;
                    }
                }
                else
                {
                    MessageBox.Show("修改失败，请重试");
                }
            }
            else
            {
                MessageBox.Show("请选择要修改的记录");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
